package com.ifast.demo.controller;


import java.net.InetAddress;
import org.xbill.DNS.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

class ResolverSlct {
	private static Vector<String> resolvers;
    private static String prefix;
    private static ResolverSlct instance = null;

    ResolverSlct() {
    	resolvers = new Vector<String>();
    	resolvers.add("8.8.8.8");
    	resolvers.add("8.8.4.4");
    	resolvers.add("114.114.114.114");
    }

    public static synchronized ResolverSlct getInstance(){

    if (instance == null) instance = new ResolverSlct();

    return instance;

    }

    public void SetDomainPrefix(String d) {
        prefix = d;
    }
    

    public static String Selector() throws Exception{
    	// select resolver  by domain prefix
    	if(resolvers!= null
    			&& prefix != null
    			&& !resolvers.isEmpty()
    			&& prefix.length() != 0)
    	{
    		int k = prefix.hashCode()%resolvers.size();

    		return resolvers.get(k);
    	}
    	else if(resolvers== null || resolvers.isEmpty() ||resolvers.size()==0)
    	{
    		return "8.8.8.8";
    	}else  	if(prefix.length() == 0)
    	{
    		return "8.8.8.8";
    	}else
    	{
    		return "8.8.8.8";    		
    	}
    }
    //InetAddress addr = Address.getByName("www.dnsjava.org");
    
    
    public static void main(String[] args) throws Exception {
    	prefix ="wwm";
    	getInstance();
    	String r = Selector();
    	System.out.println(r);
    }
}